package com.manager.entity.factory;

import cn.hutool.extra.spring.SpringUtil;
import com.manager.entity.dao.User;
import com.manager.entity.dto.SubCampusDto;
import com.manager.entity.dto.UserDto;
import com.manager.entity.vo.UserVo;
import com.manager.service.ISubCampusService;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class UserFactory {
    public static final UserConverter USER_CONVERTER = Mappers.getMapper(UserConverter.class);

    @Mapper
    public interface UserConverter {
        default User toUser(UserVo userVo){
            User user = new User();
            BeanUtils.copyProperties(userVo,user);
            return user;
        };

        List<UserDto> toListUserDto(List<User> user);

        default UserDto toUserDto(User user) {
            if (user == null) {
                return null;
            } else {
                UserDto.UserDtoBuilder userDto = UserDto.builder();
                userDto.id(user.getId());
                userDto.name(user.getName());
                userDto.info(user.getInfo());
                userDto.enable(user.getEnable());
                userDto.createTime(user.getCreateTime());
                userDto.updateTime(user.getUpdateTime());
                userDto.subCampusDto(SCIdToSubCampusDto.toSubCampusDto(user.getCId()));
                return userDto.build();
            }
        }
    }

    public interface SCIdToSubCampusDto {
        static SubCampusDto toSubCampusDto(Integer cid){
            if(cid==null){
                return null;
            }
            ISubCampusService iSubCampusService = SpringUtil.getBean(ISubCampusService.class);
            return iSubCampusService.getSubCampusById(cid);
        }
    }
}
